<?php
/**
 *
 * @author spring
 * @version 2014-9-30 上午8:54:54
 */
class Task_Updatedelivery extends Minion_Task
{
    /**
     * Generates a help list for all tasks
     *
     * @return null
     */


    protected $_options = array(
        'start'=>NULL,
        'end'=>NULL,
        'notice'=>NULL,
    );
    //php f:\wamp\www\newerp\index.php --task=delivery --start="2014-03-27 " --end="2014-03-27 23:59:28"

    protected function _execute(array $params)
    {
        set_time_limit(0);
        ini_set('memory_limit','500M');
        $start='';
        $end='';
				echo date('Y-m-d H:i:s')."开始时间！";
        if(isset($params['notice'])){
            $start=date('Y-m-d H:i:s',strtotime("-1 days"));
            $end=date('Y-m-d H:i:s');
           // $start='2014-09-22 15:40:01';
            //$end='2014-09-22 15:45:01';
        }elseif(!isset($params['start'])||!isset($params['end'])){
            $start=date('Y-m-d H:i:s',strtotime("-10 minutes"));
            $end=date('Y-m-d H:i:s');
        }else {
            $start=$params['start'];
            $end=$params['end'];
        }

        $do_time = array();
        $dos_sql = "SELECT orders_products_id,delivery_date FROM `deliveryorders`
        WHERE `type`=10 AND orders_products_id!=0 AND `delivery_date`>='".$start."' AND delivery_date<='".$end."'";
        $dos = DB::query(Database::SELECT,$dos_sql)->execute('newerp')->as_array();
        if(!empty($dos)){
            //$sku_arr = "(";
            $opid_arr = "('";
            foreach($dos as $do){
                $opid_arr .=$do['orders_products_id']."','";
                $do_time[$do['orders_products_id']] = $do['delivery_date'];
            }
            $opid_arr = rtrim($opid_arr,",'");
            $opid_arr = $opid_arr."')";

        //获取订单详情中的价格和币种
            $results = olderpclientapi::getopid($opid_arr);
            $results = json_decode($results);
            $defaultrate=DB::select('rate1')
                ->from('rates')
                ->where('name','=','$')
                ->execute('newerp')
                ->get('rate1');
            $sql_update_cost = $sql_update_single_price = $sql_update_money =$sql_update_place ='';
            foreach($results as $re){
                $rate= DB::select('rate1')->from('rates')->where('name','=',$re->currency_type)
                ->and_where('rate_time','<=',$do_time[$re->orders_products_id])
                ->order_by('rate_time','desc' )
                ->limit(1)
                ->offset(0)
                ->execute('newerp')
                ->get('rate1',$defaultrate);
                $single_price=$re->item_price*$rate;//计算销售价

                //获取成本价
                $common = new Service_common();
                $price = $common->get_sku_price(strtoupper(trim($re->orders_sku)));

                $sql_update_single_price.=" WHEN '".$re->orders_products_id."' THEN '".$single_price."'";
                $sql_update_money.=" WHEN '".$re->orders_products_id."' THEN quantity*".$single_price;
                $sql_update_cost .=" WHEN '".$re->orders_products_id."' THEN quantity*".$price;
                $sql_update_place .= " WHEN '".$re->orders_products_id."' THEN '".$re->products_place."'";
            }
            $sql_update="UPDATE deliveryorders SET single_price=(CASE orders_products_id ".$sql_update_single_price." END),
					money=(CASE orders_products_id ".$sql_update_money." END),
					cost=(CASE orders_products_id ".$sql_update_cost." END),
					storage_position=(CASE orders_products_id ".$sql_update_place." END) WHERE orders_products_id in".$opid_arr;
            $result = DB::query(Database::UPDATE,$sql_update)->execute('newerp');
        }
			echo '数据开始时间是'.$start.",数据结束时间是".$end.",执行结果是".$result."\n";
			echo date('Y-m-d H:i:s')."结束时间\n";
    }
}
